using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using BA.DAL;
using Microsoft.VisualBasic;

namespace BlazeApps.Library.Page
{
    public class PageList : Base
    {
        public DataSet GetPageList(int Type, int Status, String StartDate, String EndDate, String Search)
        {
            StringBuilder Sql = new StringBuilder();
            StringBuilder Where = new StringBuilder();

            BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
            DAL.CreateDBObjects(ConnectionString, Provider);

            Sql.Append("SELECT pageid, kbid, createdate, publishdate, expiredate, ");
            Sql.Append("statusdate, currentuserid, title, iv_access_type, ");
            Sql.Append("iv_level, iv_status, iv_priority, keywords,");
            Sql.Append("pagerevision, iv_itemtype, accesstype, difficultylevel, status, priority, itemtype, ");
            Sql.Append("summary, spotlight, userid ");
            Sql.Append("FROM vw_page_list ");

            if (Type != -1)
            {
                Where.Append("(iv_itemtype = @itemtype) AND ");
                DAL.AddParameter("@itemtype", Type);
            }

            if (Status != -1)
            {
                Where.Append("(iv_status = @status) AND ");
                DAL.AddParameter("@status", Status);
            }

            if ((StartDate != string.Empty) & (EndDate != string.Empty))
            {
                Where.Append("(createdate between @startdate and @enddate) AND ");
                DAL.AddParameter("@startdate", StartDate);
                DAL.AddParameter("@enddate", EndDate);
            }
            else if (((StartDate) != string.Empty) & (EndDate == string.Empty))
            {
                Where.Append("(createdate >= @startdate) AND ");
                DAL.AddParameter("@startdate", StartDate);
            }
            else if ((StartDate == string.Empty) & ((EndDate) != string.Empty))
            {
                Where.Append("(createdate <= @enddate) AND ");
                DAL.AddParameter("@enddate", EndDate);
            }

            if (Search != string.Empty)
            {
                Where.Append("(title like ('%' + @search + '%')) AND ");
                DAL.AddParameter("@search", Search);
            }

            if (Where.Length > 0)
            {
                Where.Insert(0, " WHERE ");

                if (Strings.Right(Where.ToString(), 4) == "AND ")
                {
                    Where.Length = Where.Length - 4;
                }
            }

            Sql.Append(Where);

            return DAL.GetDataSet(Sql.ToString(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);             
        }
    }
}
